package com.daybook.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.daybook.mapper.UserMapper;
import com.daybook.pojo.User;
import com.daybook.service.UserService;
import com.daybook.util.PageHelp;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	/**
	 * 用户模块
	 * */
	//用户注册
	public int registerNewUser(User u) {
		return userMapper.insertNewUser(u);
	}
	//登录
	public User loginUser(User u){
		return userMapper.selectByNameAndPwd(u);
	}
	//修改密码
	public int updateUserPwd(int uid,String newPwd){
		return userMapper.updateUserByPwd(uid, newPwd);
	}
	//维护信息
	public int updateUser(User u){
		return userMapper.updateUserByUid(u);
	}
	//验证用户是否存在
	public int getUserByname(String uname){
		return userMapper.selectByUserName(uname);
	}
	//获得用户折扣信息
	 public double getUserMember(int uid){
		 return userMapper.selectUserMember(uid);
	 }
	 //用户会员等级
	  public  String getUserMemberGrade(int uid){
		  return userMapper.selectUserMemberGrade(uid);
	  }
	  //修改个人信息
	  public int changeUserByUid(User user){
		  return userMapper.updateUserByUid(user);
	  }
	/**
	 *管理员模块
	 * */
	@Override
	public PageHelp getUserList(Integer pageNow,User user) {
		int totalCount=userMapper.getUserCount(user);//获取总的记录数
		 List<User> stuList=new ArrayList<User>();
		    PageHelp page=null;
		    if(pageNow!=null){
		        page=new PageHelp(totalCount, pageNow);
		        page.setPageSize(2);
		        stuList=userMapper.selectUserByPage(page.getStartPos(), page.getPageSize(),user);//从startPos开始，获取pageSize条数据
		    }else{
		        page=new PageHelp(totalCount, 1);//初始化pageNow为1
		        page.setPageSize(2);
		        stuList=userMapper.selectUserByPage(page.getStartPos(), page.getPageSize(),user);//从startPos开始，获取pageSize条数据
		    }
		    page.setList(stuList);
		    
		    return page;
	}
	//改变用户状态
	@Override
	public int changeUserState(int uid, int ustate) {
		int result=-1;
		result=userMapper.changeUserByState(uid, ustate);
		return result;
	}

}
